package com.cyc.gulimall.auth.controller;

import com.alibaba.fastjson.TypeReference;
import com.cyc.common.utils.R;
import com.cyc.common.vo.MemberResponseVo;
import com.cyc.gulimall.auth.feign.MemberFeignService;
import com.cyc.gulimall.auth.feign.ThirdpartfeignService;
import com.cyc.gulimall.auth.vo.MemberEntity;
import com.cyc.gulimall.auth.vo.UserLoginVo;
import com.cyc.gulimall.auth.vo.UserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Controller
public class LoginController {

    @Autowired
    ThirdpartfeignService thirdpartfeignService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    MemberFeignService memberFeignService;

    @GetMapping("/sms/sendCode")
    public R  sendCode(@RequestParam("phone") String phone){

        String code = UUID.randomUUID().toString().substring(0, 4);
        stringRedisTemplate.opsForValue().set("sms:"+phone,code,10, TimeUnit.MINUTES);

        thirdpartfeignService.seng(phone,code);
        return R.ok();
    }

    @PostMapping("/register")
    public String regist(@Valid UserRegisterVo vo, BindingResult result,RedirectAttributes attributes){

        if (result.hasErrors()){
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            attributes.addFlashAttribute("errors",errors);
            return "redirect:http://localhost:20000/reg.html";
        }

        //开始注册
        String code = vo.getCode();
        String s = stringRedisTemplate.opsForValue().get("sms:" + vo.getPhone());
        if (!StringUtils.isEmpty(s)){
            if (code.equals(s)){

                stringRedisTemplate.delete("sms:"+vo.getPhone());
                R r = memberFeignService.regist(vo);
                if (r.getCode()==0){
                    //注册成功
                    return "login";
                }else {
                    Map<String, String> errors=new HashMap<>();
                    errors.put("msg",r.getDate());
                    attributes.addFlashAttribute("error",errors);
                    return "redirect:http://localhost:20000/reg.html";
                }
            }else {
                Map<String, String> errors =new HashMap<>();
                errors.put("code","验证码错误");
                attributes.addFlashAttribute("errors",errors);
                return "redirect:http://localhost:20000/reg.html";
            }
        }else {
            Map<String, String> errors =new HashMap<>();
            errors.put("code","验证码过期");
            attributes.addFlashAttribute("errors",errors);
            return "redirect:http://localhost:20000/reg.html";
        }





    }


    @PostMapping("/login")
    public String login(UserLoginVo vo, RedirectAttributes attributes, HttpSession session){
        R login = memberFeignService.login(vo);
        if (login.getCode()==0){
            MemberResponseVo data = login.getData("data", new TypeReference<MemberResponseVo>() {
            });
            session.setAttribute("loginUser",data);
            return "redirect:http://localhost:10000";
        }else {
            Map<String, String> map=new HashMap<>();
            map.put("msg","用户名或密码错误");
            attributes.addFlashAttribute("error",map);
            return "redirect:http://localhost:20000/login.html";
        }

    }





}
